package com.demo.my.Controller.detail;

import com.demo.my.Entity.Employee;
import com.demo.my.Service.EmployeeService;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

@WebServlet("/OneselfUpdate")
/**
 * 修改职员信息
 * 可以扩展(指定)
 */
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Object uuid = req.getParameter("uuid");
        Object sess_update_uuid = req.getSession().getAttribute("sess_update_uuid");
        String id = req.getParameter("id");
        //region   //判断非法情况
        if(!uuid.equals(sess_update_uuid)){
            resp.setContentType("text/html;charset=utf-8");
            if(!StringUtils.isNullOrEmpty(id)){
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"重复修改，回到个人首页\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.location.reload();\n" +
                        "    },1000)\n" +
                        "</script>");
                return;
            }else {
                req.getSession().removeAttribute("user_info");
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"修改有误，重新登录\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.location.href = \"/jsp/login.jsp\";\n" +
                        "    },1000)\n" +
                        "</script>");
                return;
            }

        }
//endregion
        if(StringUtils.isNullOrEmpty(id)){
            req.getSession().removeAttribute("user_info");
            resp.getWriter().write("<script type=\"text/javascript\">\n" +
                    "\talert(\"修改有误，重新登录\")\n" +
                    "\tsetTimeout(function () {\n" +
                    "\t\twindow.location.reload();;\n" +
                    "    },1000)\n" +
                    "</script>");
            return;
        }

        req.getSession().removeAttribute("sess_update_uuid");


        String employeeNumber = req.getParameter("employeeNumber"); //工号
        String name = req.getParameter("name");  //姓名
        String gender = req.getParameter("gender"); // 性别 男or 女
        String birthday = req.getParameter("date"); // 出生日期
        String password = req.getParameter("password"); // 新密码
        String telephone = req.getParameter("telephone"); //手机
        String email = req.getParameter("email"); //e-mail
        String address = req.getParameter("address"); // 籍贯
        String education = req.getParameter("education"); // 学历
        String notes = req.getParameter("notes"); // 备注
        String departmentNumber = req.getParameter("departmentNumber");  //部门编号
        String positionNumber = req.getParameter("positionNumber");  //职称编号
        try {
            //region  防空串
            Date date = null;
            if(StringUtils.isNullOrEmpty(birthday)){

                String format = new SimpleDateFormat("yyyy-MM-dd")
                        .format(new Date());
                date = new SimpleDateFormat("yyyy-MM-dd")
                        .parse(format);
            }else {
                date = new SimpleDateFormat("yyyy-MM-dd")
                    .parse(birthday);

            }
            //endregion
            Employee info = new Employee(Integer.parseInt(id),Integer.parseInt(employeeNumber),name,gender,date
                    ,telephone,email,address,telephone,education,Integer.parseInt(departmentNumber),Integer.parseInt(positionNumber),new Date(),password,notes,null,null);
            EmployeeService.updateById(info);
            resp.getWriter().write("<script type=\"text/javascript\">\n" +
                    "\tsetTimeout(function () {\n" +
                    "\t\twindow.location.href = \"/oneselfDetail?vid="+id+"\";\n" +
                    "    },100)\n" +
                    "</script>");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
